package team.archai.nexus.boot.context;

import lombok.SneakyThrows;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import team.archai.nexus.boot.config.ArchaiNexusProperties;
import team.archai.nexus.boot.tool.Console;

import javax.annotation.Resource;
import java.net.InetAddress;

/**
 * @description: runner
 * @author: lishibin
 * @create: 2020-06-25 22:52
 **/
@Component
public class ArchaiNexusApplicationRunner implements ApplicationRunner {

    @Resource
    private ArchaiNexusProperties archaiNexusProperties;

    @Resource
    private Environment env;

    private static final String ENABLED_SSL = "server.ssl.enabled";

    @Override
    @SneakyThrows(Exception.class)
    public void run(ApplicationArguments args) throws Exception {
        //open banner
        if (archaiNexusProperties.getBanner()) {
            String port = env.getProperty("server.port");
            String protocol = "http";
            //enabled ssl  null pointer exception
            Boolean enabledSsl = env.getProperty(ENABLED_SSL, Boolean.class);
            if (null != enabledSsl && enabledSsl) {
                protocol = "https";
                port = env.getProperty("server.port");
            }
            String appName = env.getProperty("spring.application.name", "unknown");
            String localHost = InetAddress.getLocalHost().getHostAddress();
            Console.log(String.format("\n----------------------------------------------------------\n\t" +
                            "Application '%s' is running! Access URLs:\n\t" +
                            "Local: \t\t%s://localhost:%s\n\t" +
                            "External: \t%s://%s:%s\n\t" +
                            "Doc: \t%s://%s:%s/doc.html\n" +
                            "----------------------------------------------------------",
                    appName,
                    protocol, port,
                    protocol, localHost, port, protocol, localHost, port));

        }
        //open auto create table
    }
}
